function GetQueryString(name){
    let reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
    let r = window.location.search.substr(1).match(reg);
    if (r!=null){
        return decodeURI(r[2]);
    }
    return null;
}
var id = GetQueryString("id");


layui.use(['form'], function () {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.$;
    
    var dealerSelect = document.getElementById("dealer_id");
    $.ajax({
        url:'/Dealer/list',
        type: "Post",
        data: '{"page":1,"limit":15}',
        datatype:"json",
        success:function(data){
            console.log("Debug data：",data);
            let optEle1 = document.createElement("option");
            optEle1.setAttribute("value","default");
            optEle1.appendChild(document.createTextNode("===选择经销商==="));
            dealerSelect.appendChild(optEle1);

            for (var x=0 ; x<data.data.length;x++){
                let optEle1 = document.createElement("option");
                optEle1.setAttribute("value",data.data[x].dealer_uid);
                optEle1.appendChild(document.createTextNode(data.data[x].dealer));
                dealerSelect.appendChild(optEle1);

            }
            layui.form.render("select");
        },
        complete: function () { 
            $.ajax({
                url:"/Goods/view",
                type:"Get",
                data:"id=" + id,
                datatype:"json",
                async:true,
                success:(res)=>{
                    console.log("Debug Goods view:",res);
                    form.val("goods_form", {
                        "ware_house":res[0].ware_house,
                        "id": res[0].id,
                        "goods_id":res[0].goods_id,
                        "goods":res[0].goods
                        , "goods_code": res[0].goods_code
                        ,"goods_sku": res[0].goods_sku
                        ,"dealer_id":res[0].dealer_id
                        ,"fixed_price":res[0].fixed_price
                        ,"supply_price":res[0].supply_price
                        ,"remark":res[0].remark
                    });
                    layui.form.render("select");
                }
            }); 
        },
    });
});


function uuid(){
	return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,function (c) {
		var r =Math.random() * 16 | 0 ,
		 v = c == 'x' ? r: (r & 0x3|0x8);
		return v.toString(16);
	})
}

layui.use(['form'], function () {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.$;

    //监听提交
    form.on('submit(saveBtn)', function (data) {
        console.log("Debug data.field:", data.field);
        var confirm = layer.confirm("确认此次修改？？", function () {
            
            data.field.id = parseInt(data.field.id);
            $.ajax({
                url: "/Goods/update",
                type: "post",
                data: JSON.stringify([data.field]),
                datatype: "json",
                success: (res) => {
                    // layer.close(wait);
                    var alert = layer.alert(JSON.stringify(res), {
                        title: '提交结果'
                    }, function () {

                        // 关闭alert弹出层
                        layer.close(alert);

                        layer.close(confirm);

                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                },
                error: (res) => {
                    // console.log("Debug 500:", res);
                }
            })
        });
        return false;
    })
})